package cjyyp.eventloop组件;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

@Slf4j
public class eventloop处理io事件_服务端 {
    public static void main(String[] args) {
        DefaultEventLoop eventLoop = new DefaultEventLoop();
        ChannelFuture channelFuture = new ServerBootstrap()
                //参数1：只负责ServerSocketChannel的accept事件。参数2：只负责SocketChannel的读写事件
                .group(new NioEventLoopGroup(1), new NioEventLoopGroup(2))
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        //eventLoop参数，是让eventLoop负责的线程专门来处理回调函数
                        ch.pipeline().addLast(eventLoop, "handler-1", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                ByteBuf byteBuf = (ByteBuf) msg;
                                log.info("handler-1处理消息[{}]", byteBuf.toString(StandardCharsets.UTF_8));
                                ctx.fireChannelRead(msg);//todo 让消息传递给下一个handler
                            }
                            //不写默认让EventLoopGroup来处理回调函数
                        }).addLast(new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                ByteBuf byteBuf = (ByteBuf) msg;
                                Channel channel = ctx.channel();
                                ByteBuf response = ctx.alloc().buffer(30);
                                response.writeBytes(byteBuf);
                                log.info("handler-2回写消息[{}]", response.toString(StandardCharsets.UTF_8));
                                channel.writeAndFlush(response);//todo 这里的object参数一定是ByteBuf对象！！！
                                super.channelRead(ctx, msg);//相当于 ctx.fireChannelRead(msg);
                            }
                            //out是从后往前加handler
                        })/*.addLast(new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void read(ChannelHandlerContext ctx) throws Exception {
                                log.info("handler-3处理消息");
                                super.read(ctx);
                            }
                        })*/;
                    }
                })
                .bind(8080);
    }
}
